//
//  EQSwipeItem.swift
//  DSP
//
//  Created by tucici on 2024/5/17.
//

import UIKit
import TCCKit
class EQSwipeItem: UICollectionViewCell {
    var bgImg : UIImageView!
    private var collectionView : UICollectionView!
    var block : ((_ sign: SignModule)->Void)?
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        backgroundColor = .clear
        
        bgImg = UIImageView()
        
        let flowlayout = UICollectionViewFlowLayout()
        flowlayout.scrollDirection = .horizontal
        flowlayout.minimumLineSpacing = 10.0.k_w
        flowlayout.minimumInteritemSpacing = 10.0.k_h
        flowlayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 5.k_h, right: 0)
        collectionView = UICollectionView(frame:.zero,collectionViewLayout:flowlayout)
        collectionView.backgroundColor = .clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(nib:EQSliderItem.self)
        
        contentView.addSubview(bgImg)
        contentView.addSubview(collectionView)
        
        bgImg.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(10.k_h)
        }
        
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.height.equalTo(0).priority(990)
        }
    }
    
    var array = [SignModule](){
        didSet{
            guard let flowlayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else{return}
            let w = itemWidth(flowlayout,array.count,kUIScreenWidth - 60.0.k_w)
            flowlayout.itemSize = CGSize(width: w, height: 153.k_h)
            let h = collectionH(flowlayout, array, array.count)
            collectionView.reloadData()
            collectionView.snp.updateConstraints { make in
                make.height.equalTo(h).priority(990)
            }
        }
    }
}

//MARK: - UICollectionViewDataSource
extension EQSwipeItem:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.cell(EQSliderItem.self, indexPath) as! EQSliderItem
        let sign = array[indexPath.row]
        cell.sign = sign
        cell.block = { [weak self]  in
            self?.block?(sign)
        }
        return cell
    }
}
//MARK: - UICollectionViewDelegate
extension EQSwipeItem:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
        //        let sign = array[indexPath.row]
        //        if sign.select || sign.enable == false {return}
        //        sign.select = true
        //        block?(sign)
    }
}

